#include <memory>
#include <functional>
#include "UdpServer.hpp"
#include "ThreadPool.hpp"
#include "Route.hpp"

using namespace ThreadPoolModule;

using task_t = std::function<void()>;

// 利用命令行参数
// ./udpserver port
int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        std::cerr << "Usage: "<< argv[0] << " port" << std::endl;
        return 1;
    }

    uint16_t port = std::stoi(argv[1]);

    Route route;
    auto thread_pool = ThreadPool<task_t>::GetInstance();

    std::unique_ptr<UdpServer> udp_server = std::make_unique<UdpServer>(port,[&route,&thread_pool](int sockfd,const std::string& message,const InetAddr& client){
        auto task = std::bind(&Route::SendMessage,&route,sockfd,message,client); // &route表示第一个参数this指针
        thread_pool->Enqueue(task);
    });

    udp_server->Init();
    udp_server->Start();

    return 0;
}